package com.apes.autoparts.autoCode.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.autoCode.repository.EpcBbmcDzbRepository;
import com.apes.autoparts.autoCode.repository.EpcOePjmcRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: EpcOePjmcService
 * @Author fj
 * @Package com.apes.autoparts.autoCode.service
 * @Date 2023/6/8 17:23
 * @description: OE号译码库服务类
 */
@Service
public class EpcOePjmcService {

    @Autowired
    private EpcOePjmcRepository epcOePjmcRepository;

    @Autowired
    private EpcBbmcDzbRepository epcBbmcDzbRepository;

    /**
     * oe译码库根据oe号批量查询
     * @param request
     * @return
     */
    public List queryByPartnumbers(SimpleRequest request){
        JSONObject requestJO = request.getJO();
        JSONObject data = requestJO.getJSONObject("data");
        List pjmcs = new ArrayList<>();
        if (!ObjectUtils.isEmpty(data)){
            JSONArray partnumbers = data.getJSONArray("partnumbers");
            pjmcs = epcOePjmcRepository.queryByOes(partnumbers, data.getString("EPC"));
        }
        return MapUtil.mapped(pjmcs);
    }

    /**
     * 根据epc版本名称获取总数
     * @param request
     * @return
     */
    public List queryByEpcBbmcCount(SimpleRequest request){
        List epcBbmcDzbs = new ArrayList<>();
        if (!StringUtils.isEmpty(request.get("key"))){
            String key = request.get("key");
            epcBbmcDzbs = epcBbmcDzbRepository.queryByEpcBbmcCount(key);
        }
        return MapUtil.mapped(epcBbmcDzbs);
    }

}
